<template>
  <!-- 歌单列表页 -->
  <div class="music_list">
    <!-- 发现好歌单 查看更多 -->
    <div class="top">
      <div class="title">发现好歌单</div>
      <div class="more">
        <van-button round="">查看更多</van-button>
      </div>
    </div>

    <!-- 自定义滑块 -->
    <van-swipe :loop="false" :width="150" :show-indicators="false">
      <van-swipe-item v-for="(item, index) in state.musicList" :key="index">
        <div class="list_wrap">
          <span class="count">{{ formatPlayCount(item.playCount) }}</span>

          <!-- 点击图片跳转 -->
          <img :src="item.picUrl" @click="gotoItemMusic(item.id)" />
          <div class="desc">
            <span>{{ item.name }}</span>
          </div>
        </div>
      </van-swipe-item>
    </van-swipe>
  </div>
</template>

<script>
import { getMusicList } from "@/api/home";
import { reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
export default {
  setup() {
    // const $router=useRouter()
    const $router = useRouter();
    // 创建一个可变的 state
    const state = reactive({
      musicList: [],
    });

    // 挂载时获取数据
    onMounted(async () => {
      const { data } = await getMusicList();
      state.musicList = data.result;
      // console.log("data.result", data.result);
    });

    // 处理item.playCount,将其转化为万
    function formatPlayCount(num) {
      if (num > 10000) {
        return (num / 10000).toFixed(1) + "万";
      }
      return num;
    }

    // gotoItemMusic 从主页歌单列表图片去往歌单详情页
    function gotoItemMusic(id) {
      $router.push({
        path: "/itemmusic",
        query: {
          id: id,
        },
      });
    }
    // 返回数据
    return {
      state,
      formatPlayCount,
      gotoItemMusic,
    };
  },
};
</script>

<style lang="less" scoped>
.top {
  background-color: orange;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
}
.van-swipe {
  height: 150px;
  width: 100%;
  background-color: red;
  .van-swipe-item {
    padding-right: 10px;
    padding-left: 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 10px;
    }

    .list_wrap {
      width: auto;
      height: auto;
      font-size: 13px;
      .desc {
        width: "10px";
      }
    }
    .count {
      position: absolute;
      right: 5px;
      top: 3px;
      color: #fff;
    }
  }
}
</style>